package com.yami.shop.seckill.api.listener;

import com.yami.shop.bean.app.dto.SeckillSkuDto;
import com.yami.shop.bean.app.dto.SkuDto;
import com.yami.shop.bean.app.vo.ProductVO;
import com.yami.shop.bean.app.vo.SeckillVO;
import com.yami.shop.bean.enums.ProdType;
import com.yami.shop.bean.event.LoadProdActivistEvent;
import com.yami.shop.bean.order.LoadProdActivistOrder;
import com.yami.shop.common.enums.StatusEnum;
import com.yami.shop.seckill.common.model.Seckill;
import com.yami.shop.seckill.common.model.SeckillSku;
import com.yami.shop.seckill.common.service.SeckillService;
import com.yami.shop.seckill.common.service.SeckillSkuService;
import lombok.AllArgsConstructor;
import com.yami.shop.common.util.BeanUtil;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 加载商品秒杀信息
 * @author yami
 */
@Component("seckillProdActivistListener")
@AllArgsConstructor
public class LoadProdActivistListener {

    private SeckillSkuService seckillSkuService;
    private SeckillService seckillService;



    @EventListener(LoadProdActivistEvent.class)
    @Order(LoadProdActivistOrder.DEFAULT)
    public void loadProdSeckillHandle(LoadProdActivistEvent event) {
        // 不是秒杀商品，不用处理
        if (!Objects.equals(event.getProdType(), ProdType.PROD_TYPE_SECKILL.value())) {
            return;
        }
        ProductVO productVO = event.getProductVO();

        long currentTimeMillis = System.currentTimeMillis();

        // 秒杀活动信息（来自缓存）
        Seckill seckill = seckillService.getSeckillByProdId(event.getProdId());
        // 无法获取秒杀信息、秒杀没有启用、当前时间大于活动结束时间， 则不添加活动信息
        boolean unActivity = Objects.isNull(seckill) || !Objects.equals(seckill.getStatus(), StatusEnum.ENABLE.value()) || currentTimeMillis > seckill.getEndTime().getTime();
        if (unActivity) {
            return;
        }

        // 秒杀活动sku信息（来自缓存）
        List<SeckillSku> seckillSkus = seckillSkuService.listSeckillSkuBySeckillId(seckill.getSeckillId());

        // 获取秒杀sku列表
        Map<Long, SkuDto> skuMap = productVO.getSkuList().stream().collect(Collectors.toMap(SkuDto::getSkuId, s -> s));
        List<SeckillSkuDto> seckillSkuDtos = new ArrayList<>();
        for (SeckillSku seckillSku : seckillSkus) {
            SeckillSkuDto seckillSkuDto = new SeckillSkuDto();
            if (!skuMap.containsKey(seckillSku.getSkuId())) {
                continue;
            }
            SkuDto sku = skuMap.get(seckillSku.getSkuId());
            seckillSkuDto.setPic(sku.getPic());
            seckillSkuDto.setProperties(sku.getProperties());
            seckillSkuDto.setPrice(sku.getPrice());
            seckillSkuDto.setSeckillPrice(seckillSku.getSeckillPrice());
            seckillSkuDto.setSeckillStocks(seckillSku.getSeckillStocks());
            seckillSkuDto.setSkuId(sku.getSkuId());
            seckillSkuDto.setSeckillSkuId(seckillSku.getSeckillSkuId());
            seckillSkuDtos.add(seckillSkuDto);
        }

        SeckillVO seckillVO = BeanUtil.map(seckill, SeckillVO.class);
        // 插入秒杀sku列表信息
        seckillVO.setSeckillSkuList(seckillSkuDtos);
        // 插入秒杀信息
        productVO.setSeckillVO(seckillVO);
    }
}
